package com.example.floristshop.adapters;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.example.floristshop.FlowerInfoActivity;
import com.example.floristshop.R;
import com.example.floristshop.database.Flower;

import java.util.List;

public class FlowerAdapter extends RecyclerView.Adapter<FlowerAdapter.ViewHolder> {

    private List<Flower> flower_list;
    private int flower_item;
    private OnItemClickListener itemClickListener;

    //点击事件接口
    public interface OnItemClickListener{
        void onItemClick(int position);
    }
    //设置点击事件的方法
    public void setItemClickListener(OnItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }

    public FlowerAdapter(FragmentActivity activity, int _flower_item, List<Flower> _flower_list){
        flower_list=_flower_list;
        flower_item=_flower_item;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        View flowerView;
        ImageView image_iv;
        TextView name_tv, type_tv, origin_tv, intro_tv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            flowerView = itemView;
            image_iv = (ImageView)itemView.findViewById(R.id.flower_list_image);
            name_tv = (TextView)itemView.findViewById(R.id.flower_list_name);
            type_tv = (TextView)itemView.findViewById(R.id.flower_list_type);
            origin_tv = (TextView)itemView.findViewById(R.id.flower_list_origin);
            intro_tv = (TextView)itemView.findViewById(R.id.flower_list_intro);
        }
    }

    @NonNull
    @Override
    public FlowerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(
                flower_item,parent,false);
        final ViewHolder holder=new ViewHolder(view);
        holder.flowerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=holder.getAdapterPosition();
                if(itemClickListener!=null)
                    itemClickListener.onItemClick(position);

                Flower flower=flower_list.get(position);
                Intent intent = new Intent(v.getContext(), FlowerInfoActivity.class);
                intent.putExtra("flower_id", flower.getFlower_id());
                v.getContext().startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull FlowerAdapter.ViewHolder holder, int position) {
        Flower flower=flower_list.get(position);
        holder.image_iv.setImageResource(flower.getImage());
        holder.name_tv.setText(flower.getName());
        holder.type_tv.setText(flower.getType());
        holder.origin_tv.setText(flower.getOrigin());
        holder.intro_tv.setText(flower.getIntro());
    }

    @Override
    public int getItemCount() {
        return flower_list.size();
    }

}
